/**
 * @author casazaft
 */
/**
 * @class j2j.event.EventMgr
 * EventMgr defined some cross-browser apis to manage the native Event.
 */
Package("j2j.event").Class("EventMgr",{
	EventMgr:function(){
		        
        throw Error("the j2j.html.Event Class can only be use as a Static Class");
    },
    Statics: function(){
        var getTarget = function(){
            if (window.addEventListener) {
                return function(e){
                    return e.currentTarget;
                };
            }
            else 
                if (window.attachEvent) {
                    return function(e){
                        return e.srcElement;
                    };
                }
                else {
                    return function(e){
                        return null;
                    };
                }
        }();
        var fireEvent = function(target, etype, datatype, data){
            if (typeof target == "string") 
                target = document.getElementById(target);
            if (document.createEvent) {
                var e = document.createEvent("Events");
                e.initEvent(etype, false, false);
            }
            else 
                if (document.createEventObject) {
                    var e = document.createEventObject();                    
                }
                else 
                    return;
            if(datatype)e.datatype = datatype;
            if(data)e.data = data;
            if (target.dispatchEvent) 
                target.dispatchEvent(e);
            else 
                if (target.fireEvent) {
                    target.fireEvent("on" + etype, e);
                }
            
        };
        var preventDefault = function(){
            if (window.addEventListener) {
                return function(e){
                    e.preventDefault();
                };
            }
            else 
                if (window.attachEvent) {
                    return function(e){
                        e.returnValue = false;
                    };
                }
        }();
        var stopPropagation = function(){
            if (window.addEventListener) {
                return function(e){
                    e.stopPropagation();
                };
            }
            else 
                if (window.attachEvent) {
                    return function(e){
                        e.cancelBubble = true;
                    };
                }
        }();
        var stopEvent = function(e){
            stopPropagation(e);
            preventDefault(e);
        };
        var addEventListener = function(){
            if (window.addEventListener) {
                return function(obj, eventName, fn, capture){
                    obj.addEventListener(eventName, fn, capture);
                    obj = null;
                };
            }
            else 
                if (window.attachEvent) {
                    return function(obj, eventName, fn, capture){
                        obj.attachEvent("on" + eventName, fn);
                        obj = null;
                    };
                }
                else {
                    throw Error("j2j don't not support event manage in this browser!");
                }
        }();
        var removeEventListener = function(){
            if (window.addEventListener) {
                return function(obj, eventName, fn, capture){
                    obj.removeEventListener(eventName, fn, capture);
                    obj = null;
                };
            }
            else 
                if (window.attachEvent) {
                    return function(obj, eventName, fn, capture){
                        obj.detachEvent("on" + eventName, fn);
                        obj = null;
                    };
                }
                else {
                    throw Error("j2j don't not support event manage in this browser!");
                }
        }();
        return {
            fireEvent: fireEvent,
            stopEvent: stopEvent,
            addEventListener: addEventListener,
            removeEventListener: removeEventListener,
            getTarget: getTarget
        };
    }()
});
